package com.travel.controller.user;

import com.travel.common.UserContext;
import com.travel.constant.ForumConstant;
import com.travel.entity.po.UserArticle;
import com.travel.entity.po.UserComment;
import com.travel.entity.vo.CommentVo;
import com.travel.entity.vo.ForumVo;
import com.travel.result.Result;
import com.travel.service.UserArticleService;
import com.travel.service.UserCommentService;
import com.travel.service.impl.CommentVoService;
import com.travel.service.impl.ForumVoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.server.PathParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户论坛控制器
 * 处理用户论坛相关的请求
 * 
 * Author:Jasenon_ce
 * Date: 2025.04.19/下午2:55
 */
@RestController
@Api(tags = "用户论坛", description = "处理论坛文章和评论相关的操作")
@RequestMapping("/user/forum")
public class UserForumController {

    @Resource
    private UserArticleService userArticleService;

    @Resource
    private ForumVoService forumVoService;

    @Resource
    private UserCommentService userCommentService;

    @Resource
    private CommentVoService commentVoService;

    /**
     * 添加文章
     * @param article 文章信息
     * @return 操作结果
     */
    @PostMapping("/article")
    @ApiOperation(value = "发布文章", notes = "用户发布新文章，会自动关联当前登录用户ID")
    public Result<?> addAricle(
            @ApiParam(value = "文章信息", required = true) 
            @RequestBody UserArticle article){

        if (UserContext.getUserId()!=null){
            article.setUserId(UserContext.getUserId());
        }

        userArticleService.insert(article);
        return Result.success(ForumConstant.FORUMS_ARTICLE_ADD_SUCCESS);
    }

    /**
     * 获取文章列表
     * @return 文章列表
     */
    @GetMapping("/article")
    @ApiOperation(value = "获取所有文章", notes = "获取论坛所有文章列表")
    public Result<List<ForumVo>> getArticle(){
        return Result.success(forumVoService.getAllArticle());
    }

    /**
     * 删除文章
     * @param articleId 文章ID
     * @param user_id 用户ID（可选，不传则从上下文获取）
     * @param response HTTP响应对象
     * @return 操作结果
     */
    @DeleteMapping("/article")
    @ApiOperation(value = "删除文章", notes = "删除指定的文章，仅文章作者可操作")
    public Result<?> deleteArticle(
            @ApiParam(value = "文章ID", required = true) 
            @RequestParam("article_id") Long articleId,
            @ApiParam(value = "用户ID，可选，不传则使用当前登录用户") 
            @RequestParam(value = "user_id", required = false) Long user_id, 
            HttpServletResponse response){

        if (UserContext.getUserId()!=null){
            user_id = UserContext.getUserId();
        }

        if (userArticleService.delete(articleId, user_id)) {
            response.setStatus(200);
            return Result.success(ForumConstant.FORUMS_ARTICLE_DELETE_SUCCESS);
        }
        response.setStatus(403);
        return Result.error(ForumConstant.FORUMS_ARTICLE_DELETE_FAIL);
    }

    /**
     * 点赞文章
     * @param articleId 文章ID
     * @return 操作结果
     */
    @GetMapping("/like")
    @ApiOperation(value = "点赞文章", notes = "为指定文章增加点赞数")
    public Result<?> like(
            @ApiParam(value = "文章ID", required = true) 
            @PathParam("article_id") Long articleId){
        userArticleService.like(articleId);
        return Result.success();
    }

    /**
     * 添加评论
     * @param comment 评论信息
     * @return 操作结果
     */
    @PostMapping("/comment")
    @ApiOperation(value = "发布评论", notes = "对文章发表评论，会自动关联当前登录用户ID")
    public Result<?> addComment(
            @ApiParam(value = "评论信息", required = true) 
            @RequestBody UserComment comment){

        if (UserContext.getUserId()!=null){
            comment.setUserId(UserContext.getUserId());
        }

        userCommentService.insert(comment);
        return Result.success();
    }

    /**
     * 获取文章评论
     * @param articleId 文章ID
     * @return 评论列表
     */
    @GetMapping("/comment")
    @ApiOperation(value = "获取文章评论", notes = "获取指定文章的所有评论")
    public Result getComment(
            @ApiParam(value = "文章ID", required = true) 
            @PathParam("article_id") Integer articleId){

        return Result.success(commentVoService.getCommentByArticleId(articleId));

    }

//    @DeleteMapping("/comment")
//    public Result<?> deleteComment(@RequestParam("article_id") Long articleId,
//                                   @RequestParam(value = "user_id", required = false) Long user_id){
//        if (UserContext.getUserId()!=null){
//            user_id = UserContext.getUserId();
//        }
//
//        userCommentService.delete()
//    }

//    @PutMapping("/artcile")
//    public Result<?> updateArticle(@RequestBody UserArticle article){
//
//    }

}
